<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>一、canvas学习 | HJ BLOG</title><meta name="keywords" content="canvas"><meta name="author" content="娄豪杰"><meta name="copyright" content="娄豪杰"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="一、canvas 简介 1.&lt;canvas&gt;&lt;&#x2F;canvas&gt;是 HTML5 新增的，一个可以使用脚本(通常为 JavaScript)在其中绘制图像的 HTML 元素。 2.它可以用来制作照片集或者制作简单(也不是那么简单)的动画，甚至可以进行实时视频处理和渲染。 3.canvas 是由 HTML 代码配合高度和宽度属性儿定义出的可绘制区域。JavaScript 代码可以访">
<meta property="og:type" content="article">
<meta property="og:title" content="一、canvas学习">
<meta property="og:url" content="http://example.com/article/canvas.html">
<meta property="og:site_name" content="HJ BLOG">
<meta property="og:description" content="一、canvas 简介 1.&lt;canvas&gt;&lt;&#x2F;canvas&gt;是 HTML5 新增的，一个可以使用脚本(通常为 JavaScript)在其中绘制图像的 HTML 元素。 2.它可以用来制作照片集或者制作简单(也不是那么简单)的动画，甚至可以进行实时视频处理和渲染。 3.canvas 是由 HTML 代码配合高度和宽度属性儿定义出的可绘制区域。JavaScript 代码可以访">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://picabstract-preview-ftn.weiyun.com/ftn_pic_abs_v3/bb5569f85761dbfe9ec1ea3fa97bf13487f7a20aeaad526c5512bafc64ace8e1f18692497ecc79963548db9d83facd63?pictype=scale&from=30113&version=3.3.3.3&uin=851681631&fname=09_canvas.webp&size=750">
<meta property="article:published_time" content="2021-08-31T01:57:00.000Z">
<meta property="article:modified_time" content="2021-09-27T08:51:18.000Z">
<meta property="article:author" content="娄豪杰">
<meta property="article:tag" content="canvas">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://picabstract-preview-ftn.weiyun.com/ftn_pic_abs_v3/bb5569f85761dbfe9ec1ea3fa97bf13487f7a20aeaad526c5512bafc64ace8e1f18692497ecc79963548db9d83facd63?pictype=scale&from=30113&version=3.3.3.3&uin=851681631&fname=09_canvas.webp&size=750"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/article/canvas"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '一、canvas学习',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-09-27 16:51:18'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/static-butterfly/dist/css/index.min.css"><link rel="stylesheet" href="/css/custom.css"><meta name="generator" content="Hexo 5.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">70</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">17</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-book"></i><span> 阅读</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-bars"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photo"><i class="fa-fw fas fa-image"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa-fw fas fa-heart card-announcement-animation"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://picabstract-preview-ftn.weiyun.com/ftn_pic_abs_v3/bb5569f85761dbfe9ec1ea3fa97bf13487f7a20aeaad526c5512bafc64ace8e1f18692497ecc79963548db9d83facd63?pictype=scale&amp;from=30113&amp;version=3.3.3.3&amp;uin=851681631&amp;fname=09_canvas.webp&amp;size=750')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">HJ BLOG</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-book"></i><span> 阅读</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-bars"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photo"><i class="fa-fw fas fa-image"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa-fw fas fa-heart card-announcement-animation"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">一、canvas学习</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-08-31T01:57:00.000Z" title="发表于 2021-08-31 09:57:00">2021-08-31</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-09-27T08:51:18.000Z" title="更新于 2021-09-27 16:51:18">2021-09-27</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/canvas/">canvas</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="一、canvas学习"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="一、canvas-简介"><a href="#一、canvas-简介" class="headerlink" title="一、canvas 简介"></a>一、canvas 简介</h2><blockquote>
<p>1.<code>&lt;canvas&gt;&lt;/canvas&gt;</code>是 HTML5 新增的，一个可以使用脚本(通常为 JavaScript)在其中绘制图像的 HTML 元素。</p>
<p>2.它可以用来制作照片集或者制作简单(也不是那么简单)的动画，甚至可以进行实时视频处理和渲染。</p>
<p>3.canvas 是由 HTML 代码配合高度和宽度属性儿定义出的可绘制区域。JavaScript 代码可以访问该区域，类似于其他通用的二维 API，通过一套完整的绘图函数来动态生成图形。</p>
</blockquote>
<h2 id="二、canvas-基本使用"><a href="#二、canvas-基本使用" class="headerlink" title="二、canvas 基本使用"></a>二、canvas 基本使用</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">canvas</span> <span class="attr">id</span>=<span class="string">&quot;tutorial&quot;</span> <span class="attr">width</span>=<span class="string">&quot;300&quot;</span> <span class="attr">height</span>=<span class="string">&quot;300&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="2-1-canvas-元素"><a href="#2-1-canvas-元素" class="headerlink" title="2.1 canvas 元素"></a>2.1 canvas 元素</h3><p><code>&lt;canvas&gt;</code> 看起来和 <code>&lt;img&gt;</code> 标签一样，只是 <code>&lt;canvas&gt;</code> 只有两个可选的属性 <code>width、heigth</code> 属性，而没有 <code>src、alt</code> 属性。</p>
<p>如果不给 <code>&lt;canvas&gt;</code> 设置 <code>widht、height</code> 属性时，则默认 <code>width</code>为 300、<code>height</code> 为 150，单位都是 <code>px</code>。也可以使用 <code>css</code> 属性来设置宽高，但是如宽高属性和初始比例不一致，他会出现扭曲。所以，建议永远不要使用 <code>css</code> 属性来设置 <code>&lt;canvas&gt;</code> 的宽高。</p>
<p><strong>替换内容</strong></p>
<p>由于某些较老的浏览器（尤其是 IE9 之前的 IE 浏览器）或者浏览器不支持 HTML 元素 <code>&lt;canvas&gt;</code>，在这些浏览器上你应该总是能展示替代内容。</p>
<p>支持 <code>&lt;canvas&gt;</code> 的浏览器会只渲染 <code>&lt;canvas&gt;</code> 标签，而忽略其中的替代内容。不支持 <code>&lt;canvas&gt;</code> 的浏览器则 会直接渲染替代内容。****</p>
<p>用文本替换：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">canvas</span>&gt;</span>你的浏览器不支持 canvas，请升级你的浏览器。<span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>用<code>&lt;img /&gt;</code>替换</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">canvas</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;./路飞.jpg&quot;</span> <span class="attr">alt</span>=<span class="string">&quot;&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>结束标签 <code>&lt;/canvas&gt;</code> 不可省略。</p>
<p>与 <code>&lt;img&gt;</code> 元素不同，<code>&lt;canvas&gt;</code> 元素需要结束标签(<code>&lt;/canvas&gt;</code>)。如果结束标签不存在，则文档的其余部分会被认为是替代内容，将不会显示出来。</p>
<h3 id="2-2-渲染上下文-Thre-Rending-Context"><a href="#2-2-渲染上下文-Thre-Rending-Context" class="headerlink" title="2.2 渲染上下文(Thre Rending Context)"></a>2.2 渲染上下文(Thre Rending Context)</h3><p><code>&lt;canvas&gt;</code> 会创建一个固定大小的画布，会公开一个或多个<strong>渲染上下文</strong>(画笔)，使用<strong>渲染上下文</strong>来绘制和处理要展示的内容。</p>
<p>我们重点研究 2D 渲染上下文。 其他的上下文我们暂不研究，比如， WebGL 使用了基于 OpenGL ES 的 3D 上下文 (“experimental-webgl”) 。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span><br><span class="line"><span class="comment">//获得 2d 上下文对象</span></span><br><span class="line"><span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span><br></pre></td></tr></table></figure>
<h3 id="2-3-检测支持性"><a href="#2-3-检测支持性" class="headerlink" title="2.3 检测支持性"></a>2.3 检测支持性</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (canvas.getContext) &#123;</span><br><span class="line">	<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span><br><span class="line">	<span class="comment">// drawing code here</span></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">	<span class="comment">// canvas-unsupported code here</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="2-4-代码模板"><a href="#2-4-代码模板" class="headerlink" title="2.4 代码模板"></a>2.4 代码模板</h3><p>实例</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">canvas</span> <span class="attr">id</span>=<span class="string">&quot;tutorial&quot;</span> <span class="attr">width</span>=<span class="string">&quot;300&quot;</span> <span class="attr">height</span>=<span class="string">&quot;300&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span></span><br><span class="line"><span class="javascript">	<span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">		<span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span></span><br><span class="line"><span class="javascript">		<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span></span><br><span class="line"><span class="javascript">		<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span></span><br><span class="line"><span class="javascript">		<span class="comment">//开始代码</span></span></span><br><span class="line">	&#125;</span><br><span class="line">	draw();</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="2-5-一个简单的例子"><a href="#2-5-一个简单的例子" class="headerlink" title="2.5 一个简单的例子"></a>2.5 一个简单的例子</h3><p>以下实例绘制两个长方形：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;X-UA-Compatible&quot;</span> <span class="attr">content</span>=<span class="string">&quot;IE=edge&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">style</span>&gt;</span></span><br><span class="line">			canvas &#123;</span><br><span class="line">				border: 1px solid;</span><br><span class="line">			&#125;</span><br><span class="line">		<span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">canvas</span> <span class="attr">id</span>=<span class="string">&quot;tutorial&quot;</span> <span class="attr">width</span>=<span class="string">&quot;300&quot;</span> <span class="attr">height</span>=<span class="string">&quot;300&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">			<span class="comment">// 矩形：rect</span></span></span><br><span class="line"><span class="javascript">			<span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">				<span class="keyword">let</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span></span><br><span class="line"><span class="javascript">				<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span></span><br><span class="line"><span class="javascript">				<span class="keyword">let</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span></span><br><span class="line"><span class="javascript">				ctx.fillStyle = <span class="string">&#x27;rgb(200, 0, 0)&#x27;</span>;</span></span><br><span class="line"><span class="javascript">				<span class="comment">// 绘制矩形</span></span></span><br><span class="line">				ctx.fillRect(0, 0, 50, 50);</span><br><span class="line"></span><br><span class="line"><span class="javascript">				ctx.fillStyle = <span class="string">&#x27;rgba(0, 0, 200, 0.5)&#x27;</span>;</span></span><br><span class="line">				ctx.fillRect(250, 250, 50, 50);</span><br><span class="line">			&#125;</span><br><span class="line">			draw();</span><br><span class="line">		<span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="三、绘制形状"><a href="#三、绘制形状" class="headerlink" title="三、绘制形状"></a>三、绘制形状</h2><h3 id="3-1-栅格（grid）和坐标空间"><a href="#3-1-栅格（grid）和坐标空间" class="headerlink" title="3.1 栅格（grid）和坐标空间"></a>3.1 栅格（grid）和坐标空间</h3><p>如下图所示，<code>canvas</code> 元素默认被网格所覆盖。通常来说网格中的一个单元相当于 <code>canvas</code> 元素中的一像素。栅格的起点为左上角，坐标为 (0,0) 。所有元素的位置都相对于原点来定位。所以图中蓝色方形左上角的坐标为距离左边（X 轴）x 像素，距离上边（Y 轴）y 像素，坐标为 (x,y)。</p>
<p>后面我们会涉及到坐标原点的平移、网格的旋转以及缩放等。</p>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/Canvas_default_grid.png"></p>
<h3 id="3-2-绘制矩形"><a href="#3-2-绘制矩形" class="headerlink" title="3.2 绘制矩形"></a>3.2 绘制矩形</h3><p><code>&lt;canvas&gt;</code> 只支持一种原生的图形绘制：<strong>矩形</strong>。所有其他图形都至少需要生成一种路径 (<code>path</code>)。不过，我们拥有众多路径生成的方法让复杂图形的绘制成为了可能。</p>
<ul>
<li>1、<strong>fillRect(x, y, width, height)</strong> ：绘制一个填充的矩形。</li>
<li>2、<strong>strokeRect(x, y, width, height)</strong> ：绘制一个矩形的边框。</li>
<li>3、<strong>clearRect(x, y, widh, height)</strong> ：清除指定的矩形区域，然后这块区域会变的完全透明。<ul>
<li><strong>说明：</strong>这 3 个方法具有相同的参数。<ul>
<li><strong>x, y</strong>：指的是矩形的左上角的坐标。(相对于 canvas 的坐标原点)</li>
<li><strong>width, height</strong>：指的是绘制的矩形的宽和高。</li>
</ul>
</li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span><br><span class="line">	<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span><br><span class="line">	<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span><br><span class="line">	ctx.fillRect(<span class="number">10</span>, <span class="number">10</span>, <span class="number">100</span>, <span class="number">50</span>); <span class="comment">// 绘制矩形，填充的默认颜色为黑色</span></span><br><span class="line">	ctx.strokeRect(<span class="number">10</span>, <span class="number">70</span>, <span class="number">100</span>, <span class="number">50</span>); <span class="comment">// 绘制矩形边框</span></span><br><span class="line">&#125;</span><br><span class="line">draw();</span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/2400420933-5b74dd8f80306_articlex.png"></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ctx.clearRect(<span class="number">15</span>, <span class="number">15</span>, <span class="number">50</span>, <span class="number">25</span>);</span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/2347163070-5b74dd8f813a6_articlex.png"></p>
<h2 id="四、绘制路径（path）"><a href="#四、绘制路径（path）" class="headerlink" title="四、绘制路径（path）"></a>四、绘制路径（path）</h2><p>图形的基本元素是路径。</p>
<p>路径是通过不同颜色和宽度的线段或曲线相连形成的不同形状的点的集合。</p>
<p>一个路径，甚至一个子路径，都是闭合的。</p>
<p>使用路径绘制图形需要一些额外的步骤：</p>
<ol>
<li>创建路径起始点</li>
<li>调用绘制方法去绘制出路径</li>
<li>把路径封闭</li>
<li>一旦路径生成，通过描边或填充路径区域来渲染图形。</li>
</ol>
<p>下面是需要用到的方法：</p>
<ol>
<li><p><code>beginPath()</code></p>
<p>新建一条路径，路径一旦创建成功，图形绘制命令被指向到路径上生成路径</p>
</li>
<li><p><code>moveTo(x, y)</code></p>
<p>把画笔移动到指定的坐标<code>(x, y)</code>。相当于设置路径的起始点坐标。</p>
</li>
<li><p><code>closePath()</code></p>
<p>闭合路径之后，图形绘制命令又重新指向到上下文中</p>
</li>
<li><p><code>stroke()</code></p>
<p>通过线条来绘制图形轮廓</p>
</li>
<li><p><code>fill()</code></p>
<p>通过填充路径的内容区域生成实心的图形</p>
</li>
</ol>
<h3 id="4-1-绘制线段"><a href="#4-1-绘制线段" class="headerlink" title="4.1 绘制线段"></a>4.1 绘制线段</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span></span><br><span class="line">	canvas &#123;</span><br><span class="line">		border: 1px solid;</span><br><span class="line">	&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">canvas</span> <span class="attr">id</span>=<span class="string">&quot;tutorial&quot;</span> <span class="attr">width</span>=<span class="string">&quot;300&quot;</span> <span class="attr">height</span>=<span class="string">&quot;300&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">	<span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">		<span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span></span><br><span class="line"><span class="javascript">		<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span></span><br><span class="line"><span class="javascript">		<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span></span><br><span class="line"><span class="javascript">		ctx.beginPath(); <span class="comment">// 新建一条path</span></span></span><br><span class="line"><span class="javascript">		ctx.moveTo(<span class="number">10</span>, <span class="number">10</span>); <span class="comment">// 把画笔移动到指定的坐标</span></span></span><br><span class="line"><span class="javascript">		ctx.lineTo(<span class="number">200</span>, <span class="number">10</span>); <span class="comment">// 绘制一条从当前位置到指定坐标(200, 50)的直线.</span></span></span><br><span class="line"><span class="javascript">		<span class="comment">// 闭合路径。会拉一条从当前点到path起始点的直线。如果当前点与起始点重合，则什么都不做</span></span></span><br><span class="line">		ctx.closePath();</span><br><span class="line"><span class="javascript">		ctx.stroke(); <span class="comment">//绘制路径。</span></span></span><br><span class="line">	&#125;</span><br><span class="line">	draw();</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="4-2-绘制三角形边框"><a href="#4-2-绘制三角形边框" class="headerlink" title="4.2 绘制三角形边框"></a>4.2 绘制三角形边框</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span></span><br><span class="line">	canvas &#123;</span><br><span class="line">		border: 1px solid;</span><br><span class="line">	&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">canvas</span> <span class="attr">id</span>=<span class="string">&quot;tutorial&quot;</span> <span class="attr">width</span>=<span class="string">&quot;300&quot;</span> <span class="attr">height</span>=<span class="string">&quot;300&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">	<span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">		<span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span></span><br><span class="line"><span class="javascript">		<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span></span><br><span class="line"><span class="javascript">		<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span></span><br><span class="line"><span class="javascript">		ctx.beginPath(); <span class="comment">// 新建一条path</span></span></span><br><span class="line"><span class="javascript">		ctx.moveTo(<span class="number">10</span>, <span class="number">10</span>); <span class="comment">// 把画笔移动到指定的坐标</span></span></span><br><span class="line"><span class="javascript">		ctx.lineTo(<span class="number">200</span>, <span class="number">10</span>); <span class="comment">// 绘制一条从当前位置到指定坐标(200, 50)的直线.</span></span></span><br><span class="line"><span class="javascript">		ctx.lineTo(<span class="number">200</span>, <span class="number">200</span>); <span class="comment">// 绘制二条从当前位置到指定坐标(200, 200)的直线.</span></span></span><br><span class="line"><span class="javascript">		ctx.closePath(); <span class="comment">// 闭合路径。会拉一条从当前点到path起始点的直线。如果当前点与起始点重合，则什么都不做</span></span></span><br><span class="line"><span class="javascript">		ctx.stroke(); <span class="comment">// 描边。stroke不会自动closePath()</span></span></span><br><span class="line">	&#125;</span><br><span class="line">	draw();</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/2106846415-5b74dd8f67000_articlex.png"></p>
<h3 id="4-3-填充三角形"><a href="#4-3-填充三角形" class="headerlink" title="4.3 填充三角形"></a>4.3 填充三角形</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span></span><br><span class="line">	canvas &#123;</span><br><span class="line">		border: 1px solid;</span><br><span class="line">	&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">canvas</span> <span class="attr">id</span>=<span class="string">&quot;tutorial&quot;</span> <span class="attr">width</span>=<span class="string">&quot;300&quot;</span> <span class="attr">height</span>=<span class="string">&quot;300&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">	<span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">		<span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span></span><br><span class="line"><span class="javascript">		<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span></span><br><span class="line"><span class="javascript">		<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span></span><br><span class="line"><span class="javascript">		ctx.beginPath(); <span class="comment">// 新建一条path</span></span></span><br><span class="line"><span class="javascript">		ctx.moveTo(<span class="number">10</span>, <span class="number">10</span>); <span class="comment">// 把画笔移动到指定的坐标</span></span></span><br><span class="line"><span class="javascript">		ctx.lineTo(<span class="number">200</span>, <span class="number">10</span>); <span class="comment">// 绘制一条从当前位置到指定坐标(200, 50)的直线.</span></span></span><br><span class="line"><span class="javascript">		ctx.lineTo(<span class="number">200</span>, <span class="number">200</span>); <span class="comment">// 绘制二条从当前位置到指定坐标(200, 200)的直线.</span></span></span><br><span class="line"><span class="javascript">		ctx.closePath(); <span class="comment">// 闭合路径。会拉一条从当前点到path起始点的直线。如果当前点与起始点重合，则什么都不做</span></span></span><br><span class="line"><span class="javascript">		ctx.stroke(); <span class="comment">// 描边。stroke不会自动closePath()</span></span></span><br><span class="line"><span class="javascript">		ctx.fill(); <span class="comment">// 填充闭合区域。如果path没有闭合，则fill()会自动闭合路径。</span></span></span><br><span class="line">	&#125;</span><br><span class="line">	draw();</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/3457015746-5b74dd8f72860_articlex.png"></p>
<h3 id="4-4-绘制圆弧"><a href="#4-4-绘制圆弧" class="headerlink" title="4.4 绘制圆弧"></a>4.4 绘制圆弧</h3><p>有两个方法可以绘制圆弧：</p>
<p>1、<strong>arc(x, y, r, startAngle, endAngle, anticlockwise)</strong>: 以<code>(x, y)</code> 为圆心，以<code>r</code> 为半径，从 <code>startAngle</code> 弧度开始到<code>endAngle</code>弧度结束。<code>anticlosewise</code> 是布尔值，<code>true</code> 表示逆时针，<code>false</code> 表示顺时针(默认是顺时针)。</p>
<p>注意：</p>
<ol>
<li><p>这里的度数都是弧度。</p>
</li>
<li><p><code>0</code> 弧度是指的 <code>x</code> 轴正方向。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">radians = (<span class="built_in">Math</span>.PI / <span class="number">180</span>) * degrees; <span class="comment">// 角度转换成弧度</span></span><br></pre></td></tr></table></figure>
<p>2、<strong>arcTo(x1, y1, x2, y2, radius)</strong>: 根据给定的控制点和半径画一段圆弧，最后再以直线连接两个控制点。</p>
</li>
</ol>
<p>圆弧案例 1</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span><br><span class="line">	<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span><br><span class="line">	<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span><br><span class="line">	ctx.beginPath();</span><br><span class="line">	ctx.arc(<span class="number">50</span>, <span class="number">50</span>, <span class="number">40</span>, <span class="number">0</span>, <span class="built_in">Math</span>.PI / <span class="number">2</span>, <span class="literal">false</span>);</span><br><span class="line">	ctx.stroke();</span><br><span class="line">&#125;</span><br><span class="line">draw();</span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/3832141455-5b74dd8f658df_articlex.png"></p>
<p>圆弧案例 2</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span><br><span class="line">	<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span><br><span class="line">	<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span><br><span class="line">	ctx.beginPath();</span><br><span class="line">	ctx.arc(<span class="number">50</span>, <span class="number">50</span>, <span class="number">40</span>, <span class="number">0</span>, <span class="built_in">Math</span>.PI / <span class="number">2</span>, <span class="literal">false</span>);</span><br><span class="line">	ctx.stroke();</span><br><span class="line"></span><br><span class="line">	ctx.beginPath();</span><br><span class="line">	ctx.arc(<span class="number">150</span>, <span class="number">50</span>, <span class="number">40</span>, <span class="number">0</span>, -<span class="built_in">Math</span>.PI / <span class="number">2</span>, <span class="literal">true</span>);</span><br><span class="line">	ctx.closePath();</span><br><span class="line">	ctx.stroke();</span><br><span class="line"></span><br><span class="line">	ctx.beginPath();</span><br><span class="line">	ctx.arc(<span class="number">50</span>, <span class="number">150</span>, <span class="number">40</span>, -<span class="built_in">Math</span>.PI / <span class="number">2</span>, <span class="built_in">Math</span>.PI / <span class="number">2</span>, <span class="literal">false</span>);</span><br><span class="line">	ctx.fill();</span><br><span class="line"></span><br><span class="line">	ctx.beginPath();</span><br><span class="line">	ctx.arc(<span class="number">150</span>, <span class="number">150</span>, <span class="number">40</span>, <span class="number">0</span>, <span class="built_in">Math</span>.PI, <span class="literal">false</span>);</span><br><span class="line">	ctx.fill();</span><br><span class="line">&#125;</span><br><span class="line">draw();</span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/2218794221-5b74dd8f43f98_articlex.png"></p>
<p>圆弧案例 3</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span><br><span class="line">	<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span><br><span class="line">	<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span><br><span class="line">	ctx.beginPath();</span><br><span class="line">	ctx.moveTo(<span class="number">50</span>, <span class="number">50</span>);</span><br><span class="line">	<span class="comment">//参数1、2：控制点1坐标   参数3、4：控制点2坐标  参数4：圆弧半径</span></span><br><span class="line">	ctx.arcTo(<span class="number">200</span>, <span class="number">50</span>, <span class="number">200</span>, <span class="number">200</span>, <span class="number">100</span>);</span><br><span class="line">	ctx.lineTo(<span class="number">200</span>, <span class="number">200</span>);</span><br><span class="line">	ctx.stroke();</span><br><span class="line"></span><br><span class="line">	ctx.beginPath();</span><br><span class="line">	ctx.rect(<span class="number">50</span>, <span class="number">50</span>, <span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">	ctx.rect(<span class="number">200</span>, <span class="number">50</span>, <span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">	ctx.rect(<span class="number">200</span>, <span class="number">200</span>, <span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">	ctx.fill();</span><br><span class="line">&#125;</span><br><span class="line">draw();</span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/3556678928-5b74dd8f1bd2a_articlex.png"></p>
<p><code>arcTo</code> 方法的说明：</p>
<p>这个方法可以这样理解。绘制的弧形是由两条切线所决定。</p>
<p>第 1 条切线：起始点和控制点 1 决定的直线。</p>
<p>第 2 条切线：控制点 1 和控制点 2 决定的直线。</p>
<p><strong>其实绘制的圆弧就是与这两条直线相切的圆弧。</strong></p>
<h3 id="4-5-绘制贝塞尔曲线"><a href="#4-5-绘制贝塞尔曲线" class="headerlink" title="4.5 绘制贝塞尔曲线"></a>4.5 绘制贝塞尔曲线</h3><h4 id="4-5-1-什么是贝塞尔曲线"><a href="#4-5-1-什么是贝塞尔曲线" class="headerlink" title="4.5.1 什么是贝塞尔曲线"></a>4.5.1 什么是贝塞尔曲线</h4><p>贝塞尔曲线(Bézier curve)，又称贝兹曲线或贝济埃曲线，是应用于二维图形应用程序的数学曲线。</p>
<p>一般的矢量图形软件通过它来精确画出曲线，贝兹曲线由线段与节点组成，节点是可拖动的支点，线段像可伸缩的皮筋，我们在绘图工具上看到的钢笔工具就是来做这种矢量曲线的。</p>
<p>贝塞尔曲线是计算机图形学中相当重要的参数曲线，在一些比较成熟的位图软件中也有贝塞尔曲线工具如 PhotoShop 等。在 Flash4 中还没有完整的曲线工具，而在 Flash5 里面已经提供出贝塞尔曲线工具。</p>
<p>贝塞尔曲线于 1962，由法国工程师皮埃尔·贝塞尔（Pierre Bézier）所广泛发表，他运用贝塞尔曲线来为汽车的主体进行设计。贝塞尔曲线最初由 Paul de Casteljau 于 1959 年运用 de Casteljau 演算法开发，以稳定数值的方法求出贝兹曲线。</p>
<p>一次贝塞尔曲线其实是一条直线</p>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/240px-b_1_big.gif"></p>
<p>二次贝塞尔曲线</p>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/b_2_big.gif"></p>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/1544764428-5713-240px-BC3A9zier-2-big.svg-.png"></p>
<p>三次贝塞尔曲线</p>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/b_3_big.gif"></p>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/1544764428-2467-240px-BC3A9zier-3-big.svg-.png"></p>
<h4 id="4-5-2-绘制贝塞尔曲线"><a href="#4-5-2-绘制贝塞尔曲线" class="headerlink" title="4.5.2 绘制贝塞尔曲线"></a>4.5.2 绘制贝塞尔曲线</h4><p>绘制二次贝塞尔曲线:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">quadraticCurveTo(cp1x, cp1y, x, y);</span><br></pre></td></tr></table></figure>
<p>说明：</p>
<ul>
<li>参数 1 和 2：控制点坐标</li>
<li>参数 3 和 4：结束点坐标</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span><br><span class="line">	<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span><br><span class="line">	<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span><br><span class="line">	ctx.beginPath();</span><br><span class="line">	ctx.moveTo(<span class="number">10</span>, <span class="number">200</span>); <span class="comment">//起始点</span></span><br><span class="line">	<span class="keyword">var</span> cp1x = <span class="number">40</span>,</span><br><span class="line">		cp1y = <span class="number">100</span>; <span class="comment">//控制点</span></span><br><span class="line">	<span class="keyword">var</span> x = <span class="number">200</span>,</span><br><span class="line">		y = <span class="number">200</span>; <span class="comment">// 结束点</span></span><br><span class="line">	<span class="comment">//绘制二次贝塞尔曲线</span></span><br><span class="line">	ctx.quadraticCurveTo(cp1x, cp1y, x, y);</span><br><span class="line">	ctx.stroke();</span><br><span class="line"></span><br><span class="line">	ctx.beginPath();</span><br><span class="line">	ctx.rect(<span class="number">10</span>, <span class="number">200</span>, <span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">	ctx.rect(cp1x, cp1y, <span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">	ctx.rect(x, y, <span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">	ctx.fill();</span><br><span class="line">&#125;</span><br><span class="line">draw();</span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/274915666-5b74dd8ecb2e2_articlex.png"></p>
<p>绘制三次贝塞尔曲线:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);</span><br></pre></td></tr></table></figure>
<p>说明：</p>
<ul>
<li>参数 1 和 2：控制点 1 的坐标</li>
<li>参数 3 和 4：控制点 2 的坐标</li>
<li>参数 5 和 6：结束点的坐标</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span><br><span class="line">	<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span><br><span class="line">	<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span><br><span class="line">	ctx.beginPath();</span><br><span class="line">	ctx.moveTo(<span class="number">40</span>, <span class="number">200</span>); <span class="comment">//起始点</span></span><br><span class="line">	<span class="keyword">var</span> cp1x = <span class="number">20</span>,</span><br><span class="line">		cp1y = <span class="number">100</span>; <span class="comment">//控制点1</span></span><br><span class="line">	<span class="keyword">var</span> cp2x = <span class="number">100</span>,</span><br><span class="line">		cp2y = <span class="number">120</span>; <span class="comment">//控制点2</span></span><br><span class="line">	<span class="keyword">var</span> x = <span class="number">200</span>,</span><br><span class="line">		y = <span class="number">200</span>; <span class="comment">// 结束点</span></span><br><span class="line">	<span class="comment">//绘制二次贝塞尔曲线</span></span><br><span class="line">	ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);</span><br><span class="line">	ctx.stroke();</span><br><span class="line"></span><br><span class="line">	ctx.beginPath();</span><br><span class="line">	ctx.rect(<span class="number">40</span>, <span class="number">200</span>, <span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">	ctx.rect(cp1x, cp1y, <span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">	ctx.rect(cp2x, cp2y, <span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">	ctx.rect(x, y, <span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">	ctx.fill();</span><br><span class="line">&#125;</span><br><span class="line">draw();</span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/3947786617-5b74dd8ec8678_articlex.png"></p>
<h2 id="五、添加样式和颜色"><a href="#五、添加样式和颜色" class="headerlink" title="五、添加样式和颜色"></a>五、添加样式和颜色</h2><p>在前面的绘制矩形章节中，只用到了默认的线条和颜色。</p>
<p>如果想要给图形上色，有两个重要的属性可以做到。</p>
<ol>
<li><code>fillStyle = color</code> 设置图形的填充颜色</li>
<li><code>strokeStyle = color</code> 设置图形轮廓的颜色</li>
</ol>
<p><strong>备注：</strong></p>
<ul>
<li><ol>
<li>color 可以是表示 css 颜色值的字符串、渐变对象或者图案对象。</li>
</ol>
</li>
<li><ol start="2">
<li>默认情况下，线条和填充颜色都是黑色。</li>
</ol>
</li>
<li><ol start="3">
<li>一旦您设置了 strokeStyle 或者 fillStyle 的值，那么这个新值就会成为新绘制的图形的默认值。如果你要给每个图形上不同的颜色，你需要重新设置 fillStyle 或 strokeStyle 的值。</li>
</ol>
</li>
</ul>
<h3 id="fillStyle"><a href="#fillStyle" class="headerlink" title="fillStyle"></a>fillStyle</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span><br><span class="line">	<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span><br><span class="line">	<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">6</span>; i++) &#123;</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="number">6</span>; j++) &#123;</span><br><span class="line">			ctx.fillStyle =</span><br><span class="line">				<span class="string">&#x27;rgb(&#x27;</span> +</span><br><span class="line">				<span class="built_in">Math</span>.floor(<span class="number">255</span> - <span class="number">42.5</span> * i) +</span><br><span class="line">				<span class="string">&#x27;,&#x27;</span> +</span><br><span class="line">				<span class="built_in">Math</span>.floor(<span class="number">255</span> - <span class="number">42.5</span> * j) +</span><br><span class="line">				<span class="string">&#x27;,0)&#x27;</span>;</span><br><span class="line">			ctx.fillRect(j * <span class="number">50</span>, i * <span class="number">50</span>, <span class="number">50</span>, <span class="number">50</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">draw();</span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/2505008676-5b74dd8ebad41_articlex.png"></p>
<h3 id="strokeStyle"><a href="#strokeStyle" class="headerlink" title="strokeStyle"></a>strokeStyle</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span><br><span class="line">	<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span><br><span class="line">	<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">6</span>; i++) &#123;</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="number">6</span>; j++) &#123;</span><br><span class="line">			ctx.strokeStyle = <span class="string">`rgb(<span class="subst">$&#123;randomInt(<span class="number">0</span>, <span class="number">255</span>)&#125;</span>,<span class="subst">$&#123;randomInt(<span class="number">0</span>, <span class="number">255</span>)&#125;</span>,<span class="subst">$&#123;randomInt(</span></span></span><br><span class="line"><span class="string"><span class="subst">				<span class="number">0</span>,</span></span></span><br><span class="line"><span class="string"><span class="subst">				<span class="number">255</span></span></span></span><br><span class="line"><span class="string"><span class="subst">			)&#125;</span>)`</span>;</span><br><span class="line">			ctx.strokeRect(j * <span class="number">50</span>, i * <span class="number">50</span>, <span class="number">40</span>, <span class="number">40</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">draw();</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> 返回随机的 [from, to] 之间的整数(包括from，也包括to)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">randomInt</span>(<span class="params"><span class="keyword">from</span>, to</span>) </span>&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">parseInt</span>(<span class="built_in">Math</span>.random() * (to - <span class="keyword">from</span> + <span class="number">1</span>) + <span class="keyword">from</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/3288535670-5b74dd8ea12d9_articlex.png"></p>
<h3 id="Transparency-透明度"><a href="#Transparency-透明度" class="headerlink" title="Transparency(透明度)"></a>Transparency(透明度)</h3><p><strong>globalAlpha = transparencyValue</strong>: 这个属性影响到 canvas 里所有图形的透明度，有效的值范围是 0.0 （完全透明）到 1.0（完全不透明），默认是 1.0。</p>
<p><strong>globalAlpha</strong> 属性在需要绘制大量拥有相同透明度的图形时候相当高效。不过，我认为使用 rgba()设置透明度更加好一些。</p>
<h3 id="1、line-style"><a href="#1、line-style" class="headerlink" title="1、line style"></a>1、line style</h3><p>线宽。只能是正值。默认是 1.0。</p>
<p>起始点和终点的连线为中心，<strong>上下各占线宽的一半</strong>。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">ctx.beginPath();</span><br><span class="line">ctx.moveTo(<span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">ctx.lineTo(<span class="number">100</span>, <span class="number">10</span>);</span><br><span class="line">ctx.lineWidth = <span class="number">10</span>;</span><br><span class="line">ctx.stroke();</span><br><span class="line"></span><br><span class="line">ctx.beginPath();</span><br><span class="line">ctx.moveTo(<span class="number">110</span>, <span class="number">10</span>);</span><br><span class="line">ctx.lineTo(<span class="number">160</span>, <span class="number">10</span>);</span><br><span class="line">ctx.lineWidth = <span class="number">20</span>;</span><br><span class="line">ctx.stroke();</span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/3410060825-5b74dd8ea12d9_articlex.png"></p>
<h3 id="2-lineCap-type"><a href="#2-lineCap-type" class="headerlink" title="2. lineCap = type"></a>2. lineCap = type</h3><p>线条末端样式。</p>
<p>共有 3 个值：</p>
<ol>
<li><p><code>butt</code>：线段末端以方形结束</p>
</li>
<li><p><code>round</code>：线段末端以圆形结束</p>
</li>
<li><p><code>square</code>：线段末端以方形结束，但是增加了一个宽度和线段相同，高度是线段厚度一半的矩形区域。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> lineCaps = [<span class="string">&#x27;butt&#x27;</span>, <span class="string">&#x27;round&#x27;</span>, <span class="string">&#x27;square&#x27;</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</span><br><span class="line">	ctx.beginPath();</span><br><span class="line">	ctx.moveTo(<span class="number">20</span> + <span class="number">30</span> * i, <span class="number">30</span>);</span><br><span class="line">	ctx.lineTo(<span class="number">20</span> + <span class="number">30</span> * i, <span class="number">100</span>);</span><br><span class="line">	ctx.lineWidth = <span class="number">20</span>;</span><br><span class="line">	ctx.lineCap = lineCaps[i];</span><br><span class="line">	ctx.stroke();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ctx.beginPath();</span><br><span class="line">ctx.moveTo(<span class="number">0</span>, <span class="number">30</span>);</span><br><span class="line">ctx.lineTo(<span class="number">300</span>, <span class="number">30</span>);</span><br><span class="line"></span><br><span class="line">ctx.moveTo(<span class="number">0</span>, <span class="number">100</span>);</span><br><span class="line">ctx.lineTo(<span class="number">300</span>, <span class="number">100</span>);</span><br><span class="line"></span><br><span class="line">ctx.strokeStyle = <span class="string">&#x27;red&#x27;</span>;</span><br><span class="line">ctx.lineWidth = <span class="number">1</span>;</span><br><span class="line">ctx.stroke();</span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/3380216230-5b74dd8e97e85_articlex.png"></p>
</li>
</ol>
<h3 id="3-lineJoin-type"><a href="#3-lineJoin-type" class="headerlink" title="3. lineJoin = type"></a>3. lineJoin = type</h3><p>同一个 path 内，设定线条与线条间接合处的样式。</p>
<p>共有 3 个值 <code>round</code>, <code>bevel</code> 和 <code>miter</code>：</p>
<ol>
<li><p><code>round</code> 通过填充一个额外的，圆心在相连部分末端的扇形，绘制拐角的形状。 圆角的半径是线段的宽度。</p>
</li>
<li><p><code>bevel</code> 在相连部分的末端填充一个额外的以三角形为底的区域， 每个部分都有各自独立的矩形拐角。</p>
</li>
<li><p><code>miter</code>(默认) 通过延伸相连部分的外边缘，使其相交于一点，形成一个额外的菱形区域。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span><br><span class="line">	<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span><br><span class="line">	<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">var</span> lineJoin = [<span class="string">&#x27;round&#x27;</span>, <span class="string">&#x27;bevel&#x27;</span>, <span class="string">&#x27;miter&#x27;</span>];</span><br><span class="line">	ctx.lineWidth = <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; lineJoin.length; i++) &#123;</span><br><span class="line">		ctx.lineJoin = lineJoin[i];</span><br><span class="line">		ctx.beginPath();</span><br><span class="line">		ctx.moveTo(<span class="number">50</span>, <span class="number">50</span> + i * <span class="number">50</span>);</span><br><span class="line">		ctx.lineTo(<span class="number">100</span>, <span class="number">100</span> + i * <span class="number">50</span>);</span><br><span class="line">		ctx.lineTo(<span class="number">150</span>, <span class="number">50</span> + i * <span class="number">50</span>);</span><br><span class="line">		ctx.lineTo(<span class="number">200</span>, <span class="number">100</span> + i * <span class="number">50</span>);</span><br><span class="line">		ctx.lineTo(<span class="number">250</span>, <span class="number">50</span> + i * <span class="number">50</span>);</span><br><span class="line">		ctx.stroke();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">draw();</span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/1584506777-5b74dd8e82768_articlex.png"></p>
</li>
</ol>
<h3 id="4-虚线"><a href="#4-虚线" class="headerlink" title="4. 虚线"></a>4. 虚线</h3><p>用 <code>setLineDash</code> 方法和 <code>lineDashOffset</code> 属性来制定虚线样式。 <code>setLineDash</code> 方法接受一个数组，来指定线段与间隙的交替；<code>lineDashOffset</code>属性设置起始偏移量。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;tutorial&#x27;</span>);</span><br><span class="line">	<span class="keyword">if</span> (!canvas.getContext) <span class="keyword">return</span>;</span><br><span class="line">	<span class="keyword">var</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span><br><span class="line"></span><br><span class="line">	ctx.setLineDash([<span class="number">20</span>, <span class="number">5</span>]); <span class="comment">// [实线长度, 间隙长度]</span></span><br><span class="line">	ctx.lineDashOffset = -<span class="number">0</span>;</span><br><span class="line">	ctx.strokeRect(<span class="number">50</span>, <span class="number">50</span>, <span class="number">210</span>, <span class="number">210</span>);</span><br><span class="line">&#125;</span><br><span class="line">draw();</span><br></pre></td></tr></table></figure>
<p><img src="https://www.runoob.com/wp-content/uploads/2018/12/2805401035-5b74dd8e6833c_articlex.png"></p>
<p><strong>备注</strong>： getLineDash() 返回一个包含当前虚线样式，长度为非负偶数的数组。</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">娄豪杰</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/article/canvas.html">http://example.com/article/canvas.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">HJ BLOG</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/canvas/">canvas</a></div><div class="post_share"><div class="social-share" data-image="https://picabstract-preview-ftn.weiyun.com/ftn_pic_abs_v3/bb5569f85761dbfe9ec1ea3fa97bf13487f7a20aeaad526c5512bafc64ace8e1f18692497ecc79963548db9d83facd63?pictype=scale&amp;from=30113&amp;version=3.3.3.3&amp;uin=851681631&amp;fname=09_canvas.webp&amp;size=750" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/article/vue-sound-code1.html"><img class="prev-cover" src="https://img2.baidu.com/it/u=4067570870,2963075030&amp;fm=253&amp;fmt=auto&amp;app=120&amp;f=JPEG?w=640&amp;h=360" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">一、Vue源码【学习规划前言】</div></div></a></div><div class="next-post pull-right"><a href="/article/62ded8c6.html"><img class="next-cover" src="https://picabstract-preview-ftn.weiyun.com/ftn_pic_abs_v3/fdbe621f6b31fb31333381d541533fe2e8a61025b4acebb1f10c9f948a9ffce78a532557a1a789d30807b98f07b23952?pictype=scale&amp;from=30113&amp;version=3.3.3.3&amp;uin=851681631&amp;fname=10_ts.jpg&amp;size=750" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">七、ts 面向对象编程</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">娄豪杰</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">70</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">17</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/louhaojie99"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">人生就像品茗，懂得吃苦，才能回甘啊。</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81canvas-%E7%AE%80%E4%BB%8B"><span class="toc-number">1.</span> <span class="toc-text">一、canvas 简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81canvas-%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="toc-number">2.</span> <span class="toc-text">二、canvas 基本使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-canvas-%E5%85%83%E7%B4%A0"><span class="toc-number">2.1.</span> <span class="toc-text">2.1 canvas 元素</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-%E6%B8%B2%E6%9F%93%E4%B8%8A%E4%B8%8B%E6%96%87-Thre-Rending-Context"><span class="toc-number">2.2.</span> <span class="toc-text">2.2 渲染上下文(Thre Rending Context)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-%E6%A3%80%E6%B5%8B%E6%94%AF%E6%8C%81%E6%80%A7"><span class="toc-number">2.3.</span> <span class="toc-text">2.3 检测支持性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-%E4%BB%A3%E7%A0%81%E6%A8%A1%E6%9D%BF"><span class="toc-number">2.4.</span> <span class="toc-text">2.4 代码模板</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-5-%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90"><span class="toc-number">2.5.</span> <span class="toc-text">2.5 一个简单的例子</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E7%BB%98%E5%88%B6%E5%BD%A2%E7%8A%B6"><span class="toc-number">3.</span> <span class="toc-text">三、绘制形状</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-%E6%A0%85%E6%A0%BC%EF%BC%88grid%EF%BC%89%E5%92%8C%E5%9D%90%E6%A0%87%E7%A9%BA%E9%97%B4"><span class="toc-number">3.1.</span> <span class="toc-text">3.1 栅格（grid）和坐标空间</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-%E7%BB%98%E5%88%B6%E7%9F%A9%E5%BD%A2"><span class="toc-number">3.2.</span> <span class="toc-text">3.2 绘制矩形</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E7%BB%98%E5%88%B6%E8%B7%AF%E5%BE%84%EF%BC%88path%EF%BC%89"><span class="toc-number">4.</span> <span class="toc-text">四、绘制路径（path）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-%E7%BB%98%E5%88%B6%E7%BA%BF%E6%AE%B5"><span class="toc-number">4.1.</span> <span class="toc-text">4.1 绘制线段</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-%E7%BB%98%E5%88%B6%E4%B8%89%E8%A7%92%E5%BD%A2%E8%BE%B9%E6%A1%86"><span class="toc-number">4.2.</span> <span class="toc-text">4.2 绘制三角形边框</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-%E5%A1%AB%E5%85%85%E4%B8%89%E8%A7%92%E5%BD%A2"><span class="toc-number">4.3.</span> <span class="toc-text">4.3 填充三角形</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4-%E7%BB%98%E5%88%B6%E5%9C%86%E5%BC%A7"><span class="toc-number">4.4.</span> <span class="toc-text">4.4 绘制圆弧</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-5-%E7%BB%98%E5%88%B6%E8%B4%9D%E5%A1%9E%E5%B0%94%E6%9B%B2%E7%BA%BF"><span class="toc-number">4.5.</span> <span class="toc-text">4.5 绘制贝塞尔曲线</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-5-1-%E4%BB%80%E4%B9%88%E6%98%AF%E8%B4%9D%E5%A1%9E%E5%B0%94%E6%9B%B2%E7%BA%BF"><span class="toc-number">4.5.1.</span> <span class="toc-text">4.5.1 什么是贝塞尔曲线</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-5-2-%E7%BB%98%E5%88%B6%E8%B4%9D%E5%A1%9E%E5%B0%94%E6%9B%B2%E7%BA%BF"><span class="toc-number">4.5.2.</span> <span class="toc-text">4.5.2 绘制贝塞尔曲线</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E6%B7%BB%E5%8A%A0%E6%A0%B7%E5%BC%8F%E5%92%8C%E9%A2%9C%E8%89%B2"><span class="toc-number">5.</span> <span class="toc-text">五、添加样式和颜色</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#fillStyle"><span class="toc-number">5.1.</span> <span class="toc-text">fillStyle</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#strokeStyle"><span class="toc-number">5.2.</span> <span class="toc-text">strokeStyle</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Transparency-%E9%80%8F%E6%98%8E%E5%BA%A6"><span class="toc-number">5.3.</span> <span class="toc-text">Transparency(透明度)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81line-style"><span class="toc-number">5.4.</span> <span class="toc-text">1、line style</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-lineCap-type"><span class="toc-number">5.5.</span> <span class="toc-text">2. lineCap &#x3D; type</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-lineJoin-type"><span class="toc-number">5.6.</span> <span class="toc-text">3. lineJoin &#x3D; type</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E8%99%9A%E7%BA%BF"><span class="toc-number">5.7.</span> <span class="toc-text">4. 虚线</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/article/cc11b2b0.html" title="二、JavaScript设计模式【下】"><img src="https://picabstract-preview-ftn.weiyun.com/ftn_pic_abs_v3/983a2ddf4c01d0f319b391469f669eba7a1a82ba077da903e3114d4062c26cdea809b197d9cf02bb077af6272de8e1ed?pictype=scale&amp;from=30113&amp;version=3.3.3.3&amp;uin=851681631&amp;fname=12_js.jpg&amp;size=750" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="二、JavaScript设计模式【下】"/></a><div class="content"><a class="title" href="/article/cc11b2b0.html" title="二、JavaScript设计模式【下】">二、JavaScript设计模式【下】</a><time datetime="2021-09-11T16:00:00.000Z" title="发表于 2021-09-12 00:00:00">2021-09-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/cc00b2b0.html" title="一、JavaScript设计模式【上】"><img src="https://picabstract-preview-ftn.weiyun.com/ftn_pic_abs_v3/983a2ddf4c01d0f319b391469f669eba7a1a82ba077da903e3114d4062c26cdea809b197d9cf02bb077af6272de8e1ed?pictype=scale&amp;from=30113&amp;version=3.3.3.3&amp;uin=851681631&amp;fname=12_js.jpg&amp;size=750" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="一、JavaScript设计模式【上】"/></a><div class="content"><a class="title" href="/article/cc00b2b0.html" title="一、JavaScript设计模式【上】">一、JavaScript设计模式【上】</a><time datetime="2021-09-10T16:00:00.000Z" title="发表于 2021-09-11 00:00:00">2021-09-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/vue-sound-code10.html" title="十、Vue源码【内置组件篇】"><img src="https://img2.baidu.com/it/u=4067570870,2963075030&amp;fm=253&amp;fmt=auto&amp;app=120&amp;f=JPEG?w=640&amp;h=360" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="十、Vue源码【内置组件篇】"/></a><div class="content"><a class="title" href="/article/vue-sound-code10.html" title="十、Vue源码【内置组件篇】">十、Vue源码【内置组件篇】</a><time datetime="2021-09-09T16:00:00.000Z" title="发表于 2021-09-10 00:00:00">2021-09-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/vue-sound-code9.html" title="九、Vue源码【指令篇】"><img src="https://img2.baidu.com/it/u=4067570870,2963075030&amp;fm=253&amp;fmt=auto&amp;app=120&amp;f=JPEG?w=640&amp;h=360" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="九、Vue源码【指令篇】"/></a><div class="content"><a class="title" href="/article/vue-sound-code9.html" title="九、Vue源码【指令篇】">九、Vue源码【指令篇】</a><time datetime="2021-09-08T16:00:00.000Z" title="发表于 2021-09-09 00:00:00">2021-09-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/vue-sound-code8.html" title="八、Vue源码【过滤器篇】"><img src="https://img2.baidu.com/it/u=4067570870,2963075030&amp;fm=253&amp;fmt=auto&amp;app=120&amp;f=JPEG?w=640&amp;h=360" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="八、Vue源码【过滤器篇】"/></a><div class="content"><a class="title" href="/article/vue-sound-code8.html" title="八、Vue源码【过滤器篇】">八、Vue源码【过滤器篇】</a><time datetime="2021-09-07T16:00:00.000Z" title="发表于 2021-09-08 00:00:00">2021-09-08</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://picabstract-preview-ftn.weiyun.com/ftn_pic_abs_v3/bb5569f85761dbfe9ec1ea3fa97bf13487f7a20aeaad526c5512bafc64ace8e1f18692497ecc79963548db9d83facd63?pictype=scale&amp;from=30113&amp;version=3.3.3.3&amp;uin=851681631&amp;fname=09_canvas.webp&amp;size=750')"><div id="footer-wrap"><div class="copyright">&copy;2017 - 2022 By 娄豪杰</div><div class="footer_custom_text"><div>嗨，欢迎来到我的博客，希望能帮助到您！</div></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"></div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="富强,民主,文明,和谐,自由,平等,公正,法治" data-fontsize="15px" data-random="true" async="async"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config-diff',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"]):not([href="/music/"]):not([href="/no-pjax/"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener toc scroll 
  window.removeEventListener('scroll', window.tocScrollFn)

  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":150,"height":300},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>